package com.jiang.jixiang.tools;

import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.View;

import androidx.fragment.app.FragmentManager;

import com.jiang.jixiang.R;
import com.jiang.jixiang.bean.UserInfoBean;
import com.jiang.jixiang.dialog.AlarmDialog;
import com.ws.universal.tools.Constants;
import com.ws.universal.tools.bottomdialog.PopBottonDialog;
import com.ws.universal.tools.takephoto.app.TakePhoto;
import com.ws.universal.tools.takephoto.compress.CompressConfig;
import com.ws.universal.tools.takephoto.model.CropOptions;
import com.ws.universal.tools.takephoto.model.TakePhotoOptions;
import com.ws.universal.tools.takephoto.multipleimageselect.helpers.ConstantsCode;
import com.ws.universal.tools.utils.GsonUtils;
import com.ws.universal.tools.utils.cache.CacheCacheUtils;
import com.ws.universal.tools.utils.cache.FilePathUtils;

import java.io.File;

public class PubDiaUtils {
    private static PubDiaUtils sPubUiUtils;

    private PopBottonDialog selectImgDialog;// 生日选择
    private AlarmDialog mAlarmDialog;//弹框提示

    public static PubDiaUtils getInstance() {
        if (sPubUiUtils == null) {
            sPubUiUtils = new PubDiaUtils();
        }
        return sPubUiUtils;
    }

    public void saveUserInfo(String bean) {
        CacheCacheUtils.put(Constants.userInfo, bean);
    }

    public void clearUserInfo() {
        CacheCacheUtils.put(Constants.userInfo, "");
    }


    private UserInfoBean getUserInfo() {
        UserInfoBean userInfoBean = GsonUtils.parseJObject(CacheCacheUtils.getString(Constants.userInfo), UserInfoBean.class);
        if (userInfoBean != null) {
            return userInfoBean;
        } else {
            return new UserInfoBean();
        }
    }

    public synchronized void showOneBtnDialog(final Context activity, String title, String content, String rightStr, boolean isCompulsory, final PublicDiaologCallback callback) {

        mAlarmDialog = new AlarmDialog(activity);
        mAlarmDialog.setTitle(title);
        mAlarmDialog.setContent(content);
        mAlarmDialog.setLeftBtnVisibility(false);
        mAlarmDialog.dg_divider_view.setVisibility(View.GONE);
        mAlarmDialog.setRightBtnStr(rightStr);
        mAlarmDialog.setRightBtnListener(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (callback != null) {
                    callback.onConfirm();
                }
            }
        });
        mAlarmDialog.setCanceledOnTouchOutside(isCompulsory);
        mAlarmDialog.setCancelable(isCompulsory);
        mAlarmDialog.show();

    }

    public synchronized void showTwoBtnDialog(final Context context, String title, String content, String lefStr, String rigStr, final PublicDiaologCallback callback) {

        mAlarmDialog = new AlarmDialog(context);
        mAlarmDialog.setTitle(title);
        mAlarmDialog.setContent(content);
        mAlarmDialog.setLeftBtnStr(lefStr);
        mAlarmDialog.setLeftBtnListener(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (callback != null) {
                    callback.onCancel();
                }
            }
        });
        mAlarmDialog.setRightBtnStr(rigStr);
        mAlarmDialog.setRightBtnListener(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (callback != null) {
                    callback.onConfirm();
                }

            }
        });
        mAlarmDialog.setCanceledOnTouchOutside(true);
        mAlarmDialog.show();
    }

    /**
     * @param fragmentManager
     * @param context
     * @param takePhoto
     * @param limit           个数
     * @param isCrop          是否裁剪
     * @param showVideo       是否显示视频
     */
    public void showSelectImgDialog(FragmentManager fragmentManager, Context context, TakePhoto takePhoto, int limit, boolean isCrop, Boolean showVideo) {

        selectImgDialog = PopBottonDialog.create(fragmentManager).setLayoutRes(R.layout.dialog_feedback_add).setViewListener(new PopBottonDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                initSelectImgDia(v, context, takePhoto, limit, isCrop, showVideo);
            }


        }).setDimAmount(0.3f).setTag("showSelectImgDialog");

        selectImgDialog.show();

    }

    private void initSelectImgDia(View view, Context context, TakePhoto takePhoto, int limit, boolean isCrop, Boolean showVideo) {
        //相机
        view.findViewById(R.id.rlDiaFeedbackTakePicture).setOnClickListener(view1 -> {
            dismissSelectImgDialog();
            openFile(1, context, takePhoto, limit, isCrop, showVideo);
        });
        //相册
        view.findViewById(R.id.rlDiaFeedbackSelectAlbum).setOnClickListener(view1 -> {
            dismissSelectImgDialog();
            openFile(2, context, takePhoto, limit, isCrop, showVideo);
        });
        view.findViewById(R.id.rlDiaFeedbackCancel).setOnClickListener(view1 -> {
            dismissSelectImgDialog();
        });

    }

    //照片选择
    private void dismissSelectImgDialog() {
        selectImgDialog.dismiss();
    }

    private void openFile(int imgType, Context context, TakePhoto takePhoto, int limit, boolean isCrop, Boolean showVideo) {
        File file = new File(FilePathUtils.getInstance().getCache(), ConstantsCode.temporaryFolder + System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        //LogcatUtils.e("  "," openFile file getPath "+file.getPath());
        //LogcatUtils.e("  "," openFile file getAbsolutePath "+file.getAbsolutePath());
        Uri imageUri = Uri.fromFile(file);

        configCompress(takePhoto);
        configTakePhotoOption(takePhoto);

        if (imgType == 1) {
            //拍照  参数为 getCropOptions 表示裁剪 不传则不裁剪 有的手机不支持裁剪
            // takePhoto.onPickFromCapture(imageUri);
            if (isCrop) {
                takePhoto.onPickFromCaptureWithCrop(imageUri, getCropOptions());

            } else {
                takePhoto.onPickFromCapture(imageUri);
            }

        } else {
            //takePhoto.onPickFromGalleryWithCrop(imageUri, getCropOptions());  有的手机不支持裁剪
            if (isCrop) {
                takePhoto.onPickMultipleWithCrop(getCropOptions(), showVideo);
            } else {
                takePhoto.onPickMultiple(limit, showVideo);
            }

        }

    }

    //裁剪
    private CropOptions getCropOptions() {
        //        if (rgCrop.getCheckedRadioButtonId() != R.id.rbCropYes) {
        //            return null;
        //        }
        //默认裁剪 不裁剪 在这里 定义参数 返回 return null
        CropOptions.Builder builder = new CropOptions.Builder();
        //builder.setOutputX(800).setOutputY(800);//矩形裁剪 同时设置尺寸/比例
        builder.setAspectX(800).setAspectY(800);//圆形裁剪 只对setWithOwnCrop为false的情况下有效 同时设置尺寸/比例
        builder.setWithOwnCrop(false);//裁剪工具 false为第三方 ture 为takePhoto自带
        return builder.create();
    }

    private void configCompress(TakePhoto takePhoto) {

        int maxSize = 102400;//大小不超过102400
        int width = 800;
        int height = 800;

        CompressConfig config;
        //自带压缩
        config = new CompressConfig.Builder().setMaxSize(maxSize).setMaxPixel(width >= height ? width : height).enableReserveRaw(false)//拍照压缩后是否保存原图
                .create();
        //luban压缩
        //        LubanOptions option = new LubanOptions.Builder().setMaxHeight(height).setMaxWidth(width).setMaxSize(maxSize).create();
        //        config = CompressConfig.ofLuban(option);
        //        config.enableReserveRaw(enableRawFile);

        takePhoto.onEnableCompress(config, true);// showCompressDialog 是否显示压缩进度


    }

    private void configTakePhotoOption(TakePhoto takePhoto) {
        TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder();
        builder.setCorrectImage(true);
        takePhoto.setTakePhotoOptions(builder.create());

    }

    public interface PublicDiaologCallback {
        void onConfirm();

        void onCancel();
    }
}
